const router = require('koa-router')()

router.prefix('/newscomment')

//projectwork 
//根据collect_id=>newsid获取newscomment详情
router.get('/single', async (ctx, next) => {
  console.log(ctx.request.body);
  let newsid = ctx.request.body.newsid;
  let result = await ctx.db.EXCUTE('SELECT * FROM newscomment WHERE newsid=' + newsid);
  console.log(result);
  if (result[0]) {
    ctx.body = {
      code: 200,
      data: result,
      msg: '存在newscomment'
    }
  } else {
    ctx.body = {
      code: 200,
      data: result,
      msg: '暂时还没有评论喔'
    }
  }
})

router.post('/comment', async (ctx, next) => {
  if (ctx.request.body.parentid) {
    let sql = 'INSERT INTO newscomment(userid,newsid,parent_comment_id,content,create_time) values(?,?,?,?,?)';
    await ctx.db.EXCUTE(sql, [ctx.request.body.userid, ctx.request.body.newsid, ctx.request.body.parentid, ctx.request.body.content, ctx.request.body.addtime]);
    ctx.body = {
      code: 200
    }
  } else {
    let sql = 'INSERT INTO newscomment(userid,newsid,content,create_time) values(?,?,?,?)';
    await ctx.db.EXCUTE(sql, [ctx.request.body.userid, ctx.request.body.newsid, ctx.request.body.content, ctx.request.body.addtime]);
    ctx.body = {
      code: 200
    }
  }
})


//backend
//页面初始化时获取数据库内的数据条目
router.post('/getnewscommentbackend', async (ctx, next) => {
  let resultuserid = await ctx.db.EXCUTE("SELECT (SELECT username FROM user ni WHERE n.userid=ni.userid) AS userinfo FROM newscomment n")
  let resultnewsid = await ctx.db.EXCUTE("SELECT (SELECT newstext FROM news ni WHERE n.newsid=ni.newsid) AS newsinfo FROM newscomment n")
  let result = await ctx.db.EXCUTE("SELECT n.*, (SELECT content FROM newscomment ni WHERE n.parent_comment_id=ni.newscom_id) AS parent_comment_info FROM newscomment n")

  for (let i = 0; i < result.length; i++) {
    Object.assign(result[i], resultuserid[i]);
    Object.assign(result[i], resultnewsid[i]);
  }

  ctx.body = {
    code: 200,
    data: result
  }
})

//审核操作
router.post('/newscommentauditbackend', async (ctx, next) => {
  let newscom_id = ctx.request.body.newscom_id;
  let sqlaudit = 'UPDATE newscomment SET isAudit = 1 WHERE newscom_id=?'
  await ctx.db.EXCUTE(sqlaudit, [newscom_id]);

  let resultuserid = await ctx.db.EXCUTE("SELECT (SELECT username FROM user ni WHERE n.userid=ni.userid) AS userinfo FROM newscomment n")
  let resultnewsid = await ctx.db.EXCUTE("SELECT (SELECT newstext FROM news ni WHERE n.newsid=ni.newsid) AS newsinfo FROM newscomment n")
  let result = await ctx.db.EXCUTE("SELECT n.*, (SELECT content FROM newscomment ni WHERE n.parent_comment_id=ni.newscom_id) AS parent_comment_info FROM newscomment n")

  for (let i = 0; i < result.length; i++) {
    Object.assign(result[i], resultuserid[i]);
    Object.assign(result[i], resultnewsid[i]);
  }

  ctx.body = {
    code: 200,
    data: result
  }
})

//审核不通过操作
router.post('/newscommentauditFailedbackend', async (ctx, next) => {
  let newscom_id = ctx.request.body.newscom_id;
  let sqlaudit = 'DELETE FROM newscomment WHERE newscom_id=?'
  await ctx.db.EXCUTE(sqlaudit, [newscom_id]);

  let resultuserid = await ctx.db.EXCUTE("SELECT (SELECT username FROM user ni WHERE n.userid=ni.userid) AS userinfo FROM newscomment n")
  let resultnewsid = await ctx.db.EXCUTE("SELECT (SELECT newstext FROM news ni WHERE n.newsid=ni.newsid) AS newsinfo FROM newscomment n")
  let result = await ctx.db.EXCUTE("SELECT n.*, (SELECT content FROM newscomment ni WHERE n.parent_comment_id=ni.newscom_id) AS parent_comment_info FROM newscomment n")

  for (let i = 0; i < result.length; i++) {
    Object.assign(result[i], resultuserid[i]);
    Object.assign(result[i], resultnewsid[i]);
  }

  ctx.body = {
    code: 200,
    data: result
  }
})

router.post('/delete', async (ctx, next) => {
  let newscom_id = ctx.request.body.newscom_id;
  let sql = 'delete from newscomment where newscom_id = ?';
  let result = await ctx.db.EXCUTE(sql, [newscom_id]);
  let sql1 = 'delete from newscomment where parent_comment_id = ?';
  let result1 = await ctx.db.EXCUTE(sql1, [newscom_id]);
  // console.log(result1, '是我是我');
  if (result.affectedRows != 0) {
    ctx.body = {
      code: 200,
    }
  }
})

module.exports = router